package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import shoppingmall.reception.dto.ProductOrderVO;
import shoppingmall.reception.mapper.CommercialSpecificationMapper;
import shoppingmall.reception.mapper.ProductMapper;
import shoppingmall.reception.pojo.CommercialSpecification;
import shoppingmall.reception.pojo.Product;
import shoppingmall.reception.pojo.ProductOrder;

import shoppingmall.reception.mapper.ProductOrderMapper;
import shoppingmall.reception.service.ProductOrderService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 平台商品订单相关服务层实现类
 */
@SuppressWarnings({"all"})
@Service
public class ProductOrderServiceImpl implements ProductOrderService {

    @Autowired
    private CommercialSpecificationMapper commercialSpecificationMapper;  //商品规格mapper层实例
    @Autowired
    private ProductMapper productMapper;  //商品mapper层实例
    @Autowired
    private ProductOrderMapper productOrderMapper;  //mapper层实例

    /**
     * 添加一条商品订单数据
     * @param productOrder
     * @return
     */
    @Override
    public List<String> saveProductOrder(ProductOrder productOrder) {
        //生成商品订单的订单号
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
        Date today = new Date();
        String orderId = dateFormat.format(today) + today.getTime() + Integer.toString((int)(100 + Math.random() * (999 - 100 + 1)));  //订单号 = 年月日时分 + 时间戳 + 随机三位数
        //准备返回的ArrayList订单号数组
        List<String> orderIds = new ArrayList<>();
        //调用mapper层方法
        try {
            productOrder.setOrderId(orderId);  //设置订单号
            productOrderMapper.saveProductOrder(productOrder);
            orderIds.add(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return orderIds;  //返回订单号数据(一个元素)
    }

    /**
     * 批量创建商品订单数据(用户购物车勾选多个商品进行结算)
     * @param productOrders
     * @return
     */
    @Override
    public List<String> saveProductOrderBatch(List<ProductOrder> productOrders) {
        //准备返回的订单号数组信息
        List<String> orderIds = new ArrayList<>();
        //处理订单号
        for (int i = 0; i < productOrders.size(); i++) {
            //生成商品订单的订单号
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
            Date today = new Date();
            String orderId = dateFormat.format(today) + today.getTime() + Integer.toString((int)(100 + Math.random() * (999 - 100 + 1)));  //订单号 = 年月日时分 + 时间戳 + 随机三位数
            productOrders.get(i).setOrderId(orderId);
            //收集用于返回的订单号信息
            orderIds.add(orderId);
        }
        //调用mapper层方法
        try {
            productOrderMapper.saveProductOrderBatch(productOrders);  //批量存储订单数据
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return orderIds;
    }

    /**
     * 查询某用户的所有订单数据列表 user_id = ?
     * @param userId
     * @return
     */
    @Override
    public List<ProductOrderVO> getUserAllProductOrderList(int userId) {
        //调用mapper层方法查询用户的所有订单信息，并将商品信息、所选规格信息追加上去
        List<ProductOrder> productOrderList = productOrderMapper.getUserAllProductOrderList(userId);
        //准备返回的对象
        List<ProductOrderVO> productOrderVOS = new ArrayList<>();
        for (int i = 0; i < productOrderList.size(); i++){
            //查询商品信息
            Product productInfo = productMapper.getProductInfoByProductId(productOrderList.get(i).getCommodityId());
            //查询选择的商品规格信息
            CommercialSpecification standardInfo = commercialSpecificationMapper.getCommercialSpecificationById(productOrderList.get(i).getCommodityStandardId());
            //给ProductOrderVO实例设置数据
            ProductOrderVO productOrderVO = new ProductOrderVO();
            productOrderVO.setId(productOrderList.get(i).getId());
            productOrderVO.setOrderId(productOrderList.get(i).getOrderId());
            productOrderVO.setCommodityId(productOrderList.get(i).getCommodityId());
            productOrderVO.setProductInfo(productInfo);  //商品信息
            productOrderVO.setUserId(productOrderList.get(i).getUserId());
            productOrderVO.setCommodityStandardId(productOrderList.get(i).getCommodityStandardId());
            productOrderVO.setProductStandardInfo(standardInfo);  //选择的商品规格信息
            productOrderVO.setCommodityStandardStr(productOrderList.get(i).getCommodityStandardStr());
            productOrderVO.setCommodityCount(productOrderList.get(i).getCommodityCount());
            productOrderVO.setOrderTotalPrice(productOrderList.get(i).getOrderTotalPrice());
            productOrderVO.setOrderState(productOrderList.get(i).getOrderState());
            productOrderVO.setLinkUser(productOrderList.get(i).getLinkUser());
            productOrderVO.setLinkPhone(productOrderList.get(i).getLinkPhone());
            productOrderVO.setLinkAddress(productOrderList.get(i).getLinkAddress());
            productOrderVO.setLogisticsInfo(productOrderList.get(i).getLogisticsInfo());
            productOrderVO.setOrderRemark(productOrderList.get(i).getOrderRemark());
            productOrderVO.setCreateTime(productOrderList.get(i).getCreateTime());
            productOrderVO.setUpdateTime(productOrderList.get(i).getUpdateTime());
            //添加进ProductOrderVOS
            productOrderVOS.add(productOrderVO);
        }
        //返回数据
        return productOrderVOS;
    }

    /**
     * 查询某条订单数据(根据订单id = ? 或者 order_id = ?)
     * @param orderId
     * @return
     */
    @Override
    public List<ProductOrderVO> getProductOrderByOrderId(String orderId) {
        //调用mapper层方法,查询订单数据,并在订单上添加商品信息和规格信息
        //当前订单基本数据
        ProductOrder productOrder = productOrderMapper.getProductOrderByOrderId(orderId);
        //查询商品信息
        Product productInfo = productMapper.getProductInfoByProductId(productOrder.getCommodityId());
        //查询选择的商品规格信息
        CommercialSpecification standardInfo = commercialSpecificationMapper.getCommercialSpecificationById(productOrder.getCommodityStandardId());
        //返回结果
        ProductOrderVO productOrderVO = new ProductOrderVO();
        productOrderVO.setId(productOrder.getId());
        productOrderVO.setOrderId(productOrder.getOrderId());
        productOrderVO.setCommodityId(productOrder.getCommodityId());
        productOrderVO.setProductInfo(productInfo);  //商品信息
        productOrderVO.setUserId(productOrder.getUserId());
        productOrderVO.setCommodityStandardId(productOrder.getCommodityStandardId());
        productOrderVO.setProductStandardInfo(standardInfo);  //选择的商品规格信息
        productOrderVO.setCommodityStandardStr(productOrder.getCommodityStandardStr());
        productOrderVO.setCommodityCount(productOrder.getCommodityCount());
        productOrderVO.setOrderTotalPrice(productOrder.getOrderTotalPrice());
        productOrderVO.setOrderState(productOrder.getOrderState());
        productOrderVO.setLinkUser(productOrder.getLinkUser());
        productOrderVO.setLinkPhone(productOrder.getLinkPhone());
        productOrderVO.setLinkAddress(productOrder.getLinkAddress());
        productOrderVO.setLogisticsInfo(productOrder.getLogisticsInfo());
        productOrderVO.setOrderRemark(productOrder.getOrderRemark());
        productOrderVO.setCreateTime(productOrder.getCreateTime());
        productOrderVO.setUpdateTime(productOrder.getUpdateTime());
        //返回数据
        List<ProductOrderVO> productOrderVOS = new ArrayList<>();
        productOrderVOS.add(productOrderVO);
        return productOrderVOS;
    }

    /**
     * 查询用户的多条订单数据
     */
    @Override
    public List<ProductOrderVO> getProductOrderByOrderIds(String orderIds) {
        //处理参数
        String[] orderIdList = orderIds.split(",");
        //调用mapper层方法批量查询订单数据，对订单数据加上商品信息及选择的规格信息
        List<ProductOrder> productOrderList = productOrderMapper.getProductOrderByOrderIds(orderIdList);  //查找订单基本信息集合
        //准备返回的对象
        List<ProductOrderVO> productOrderVOS = new ArrayList<>();
        for (int i = 0; i < productOrderList.size(); i++){
            //查询商品信息
            Product productInfo = productMapper.getProductInfoByProductId(productOrderList.get(i).getCommodityId());
            //查询选择的商品规格信息
            CommercialSpecification standardInfo = commercialSpecificationMapper.getCommercialSpecificationById(productOrderList.get(i).getCommodityStandardId());
            //给ProductOrderVO实例设置数据
            ProductOrderVO productOrderVO = new ProductOrderVO();
            productOrderVO.setId(productOrderList.get(i).getId());
            productOrderVO.setOrderId(productOrderList.get(i).getOrderId());
            productOrderVO.setCommodityId(productOrderList.get(i).getCommodityId());
            productOrderVO.setProductInfo(productInfo);  //商品信息
            productOrderVO.setUserId(productOrderList.get(i).getUserId());
            productOrderVO.setCommodityStandardId(productOrderList.get(i).getCommodityStandardId());
            productOrderVO.setProductStandardInfo(standardInfo);  //选择的商品规格信息
            productOrderVO.setCommodityStandardStr(productOrderList.get(i).getCommodityStandardStr());
            productOrderVO.setCommodityCount(productOrderList.get(i).getCommodityCount());
            productOrderVO.setOrderTotalPrice(productOrderList.get(i).getOrderTotalPrice());
            productOrderVO.setOrderState(productOrderList.get(i).getOrderState());
            productOrderVO.setLinkUser(productOrderList.get(i).getLinkUser());
            productOrderVO.setLinkPhone(productOrderList.get(i).getLinkPhone());
            productOrderVO.setLinkAddress(productOrderList.get(i).getLinkAddress());
            productOrderVO.setLogisticsInfo(productOrderList.get(i).getLogisticsInfo());
            productOrderVO.setOrderRemark(productOrderList.get(i).getOrderRemark());
            productOrderVO.setCreateTime(productOrderList.get(i).getCreateTime());
            productOrderVO.setUpdateTime(productOrderList.get(i).getUpdateTime());
            //添加进ProductOrderVOS
            productOrderVOS.add(productOrderVO);
        }
        //返回数据
        return productOrderVOS;
    }

    /**
     * 查询平台会员的相关状态订单 user_id = ? order_state = ?
     * @param userId
     * @param orderState
     * @return
     */
    @Override
    public List<ProductOrderVO> getProductOrderListByUserIdAndOrderState(int userId, String orderState) {
        //调用mapper层方法查询相关用户的相关状态的订单，并将订单商品xinx、规格信息追加上去
        //获取订单基本信息
        List<ProductOrder> productOrderList = productOrderMapper.getProductOrderListByUserIdAndOrderState(userId, orderState);
        //准备返回的对象
        List<ProductOrderVO> productOrderVOS = new ArrayList<>();
        for (int i = 0; i < productOrderList.size(); i++){
            //查询商品信息
            Product productInfo = productMapper.getProductInfoByProductId(productOrderList.get(i).getCommodityId());
            //查询选择的商品规格信息
            CommercialSpecification standardInfo = commercialSpecificationMapper.getCommercialSpecificationById(productOrderList.get(i).getCommodityStandardId());
            //给ProductOrderVO实例设置数据
            ProductOrderVO productOrderVO = new ProductOrderVO();
            productOrderVO.setId(productOrderList.get(i).getId());
            productOrderVO.setOrderId(productOrderList.get(i).getOrderId());
            productOrderVO.setCommodityId(productOrderList.get(i).getCommodityId());
            productOrderVO.setProductInfo(productInfo);  //商品信息
            productOrderVO.setUserId(productOrderList.get(i).getUserId());
            productOrderVO.setCommodityStandardId(productOrderList.get(i).getCommodityStandardId());
            productOrderVO.setProductStandardInfo(standardInfo);  //选择的商品规格信息
            productOrderVO.setCommodityStandardStr(productOrderList.get(i).getCommodityStandardStr());
            productOrderVO.setCommodityCount(productOrderList.get(i).getCommodityCount());
            productOrderVO.setOrderTotalPrice(productOrderList.get(i).getOrderTotalPrice());
            productOrderVO.setOrderState(productOrderList.get(i).getOrderState());
            productOrderVO.setLinkUser(productOrderList.get(i).getLinkUser());
            productOrderVO.setLinkPhone(productOrderList.get(i).getLinkPhone());
            productOrderVO.setLinkAddress(productOrderList.get(i).getLinkAddress());
            productOrderVO.setLogisticsInfo(productOrderList.get(i).getLogisticsInfo());
            productOrderVO.setOrderRemark(productOrderList.get(i).getOrderRemark());
            productOrderVO.setCreateTime(productOrderList.get(i).getCreateTime());
            productOrderVO.setUpdateTime(productOrderList.get(i).getUpdateTime());
            //添加进ProductOrderVOS
            productOrderVOS.add(productOrderVO);
        }
        //返回数据
        return productOrderVOS;
    }

    /**
     * 更新某订单的相关信息(主要是 订单联系人、订单联系人号码、订单联系人地址、订单备注)
     * @param productOrder
     * @return
     */
    @Override
    public boolean updateProductOrder(ProductOrder productOrder) {
        //调用mapper层方法
        try {
            productOrderMapper.updateProductOrder(productOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除平台会员订单(支持单条删除 和 多条删除) 根据订单id主键? 订单号?
     * @param orderIds
     * @return
     */
    @Override
    public boolean deleteProductOrderBatch(List<String> orderIds) {
        //调用mapper层方法
        try {
            productOrderMapper.deleteProductOrderBatch(orderIds);
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
